<?php
session_start();
//phpinfo();
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>汽车之家论坛</title>
    <link href="css/slide.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="singup/css/style.css"/>
    <link rel="stylesheet" type="text/css" href="css/easydialog.css"/>
    <link rel="stylesheet" type="text/css" href="css/easyui.css">
    <link rel="stylesheet" type="text/css" href="css/icon.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div class="top">
    <?php include_once ("TOP.php") ?>
    <?php include_once ('INFO.php');?>
    <div class="mainContent">
        <ul id="realContent">
            <li>
                <ul class="topic1">
                    <li style="float: left;width: 30px;"><img src="img/topnull.png"> </li>
                    <li style="width:50%"><b>帖子标题</b></li>
                    <li style="width:15%"><b>作者/发布时间</b></li>
                    <li style="width:15%"><b>回复/点击</b></li>
                    <li style="width:15%"><b>最后回复</b></li>

                </ul>
                <div class="clear"></div>
            </li>

        </ul>
            <div class="count">本坛帖子总数：<span id="total1"></span>
            <span id="showNewPost">
            <?php
            if ((isset($_SESSION['loginUser']) and $_SESSION['loginUser'] != '') or (isset($_COOKIE['hasLogedUsername']) and $_COOKIE['hasLogedUsername'] != '')) {
            ?>
            <span class="newPost" onclick="location.href='newPost.php';">发新贴</span>
            </span>
        <?php } ?>
        </div>
        <!--<div class="newPost" onclick="getMore();">查看更多</div>-->
    </div>
    <div class="clear"></div>
<!--    <div class="more" id="more" style=" cursor: pointer; font-size: 16px; line-height: 25px; color: #ffffff; padding: 5px; background-color: #aa5500;  display: block; width: 130px;margin: 15px auto; text-align: center; ">点击加载更多</div>
-->    <div class="bot">版权所有 汽车之家</div>
    <div id="registerForm" class="form_wrapper" style="width: 550px;">
        <form class="register">
            <h3>用户注册</h3>
            <div class="column">
                <div>
                    <label>用户名:</label>
                    <input type="text" name="username" id="username"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>昵称:</label>
                    <input type="text" name="nickname" id="nickname"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>出生日期:</label>
                    <div class="birthday"><input type="text" name="birthday" id="birthday"/>
                        <span class="error">This is an error</span></div>
                </div>
            </div>
            <div class="column">
                <div>
                    <label>选择头像:</label>
                    <input type="file" name="pic" id="pic"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>电子信箱:</label>
                    <input type="text" name="email" id="email"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>密码:</label>
                    <input type="password" name="pw" id="pw"/>
                    <span class="error">This is an error</span>
                </div>
            </div>
            <div class="bottom">
                <div class="remember"></div>
                <input type="button" value="注册" onclick="singup();"/>
                <a href="javascript:void(0);" rel="login" class="linkform" id="showLogin">已经有帐号了? 点击登录</a>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        </form>
        <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <div id="loginFrom" class="form_wrapper">
        <form class="login">
            <h3>用户登录</h3>
            <div>
                <label>用户名:</label>
                <input type="text" id="username2"/>
                <span class="error">This is an error</span>
            </div>
            <div>
                <label>密码: <a href="forgot_password.html" rel="forgot_password"
                              class="forgot linkform">忘记密码?</a></label>
                <input type="password" id="pw2"/>
                <span class="error">This is an error</span>
            </div>
            <div class="bottom">
                <div class="remember"><input type="checkbox" id="rememberMe"/><span>保存登录状态</span></div>
                <input type="button" value="登录" onclick="login();">
                <a href="javascript:void(0);" rel="register" class="linkform" id="showRegister">还没有帐号? 点击注册</a>
                <div class="clear"></div>
            </div>
        </form>
    </div>
    <div class="clear"></div>
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.form.min.js"></script>
    <script src="js/jquery.easyui.min.js"></script>
    <script src="js/responsiveslides.min.js"></script>
    <script src="js/easydialog.min.js"></script>
    <script src="js/easyui-lang-zh_CN.js"></script>
    <script src="js/site.js"></script>
    <script>
        var hasLogin = 0, hasLoggedUserName = "";
        var rememberMe = 0;
        var startPage = 0;
        var perPage = 5;
        var noData = 0; //是否请求完所有数据。默认还有数据
        var isAdmin = 0;//默认不是管理员
       <?php
            if(isset($_SESSION['loginUser']) and $_SESSION['loginUser'] != ""){ //说明已经处于登录状态
                echo "hasLogin = 1;";
                echo "hasLoggedUserName = '".$_SESSION['loginUser']."';";
            }
            else if(isset($_COOKIE['hasLoggedUserName']) and $_COOKIE['hasLoggedUserName'] != ""){ //说明前一次登录时，保存了登录状态
                echo "hasLogin = 1;";
                echo "hasLoggedUserName = '".$_COOKIE['hasLoggedUserName']."';";
            }
           if(isset($_SESSION['admin']) and $_SESSION['admin'] != ""){ //说明是管理员登录了
               echo "isAdmin = 1;";
           }
        ?>
        function getMore() {
            if(noData == 1){
                $(window).unbind("scroll");
                //alert("暂无更多数据");
                return;
            }
            getData(startPage, perPage);
        }
        function singup(){
            var errLog = 0;
            var errMsg = "";
            if($("#username").val().trim() == ""){
                errLog = 1;
                errMsg += "用户名必须填写！\n";
            }
            if($("#pw").val().trim() == ""){
                errLog = 1;
                errMsg += "密码必须填写！\n";
            }
            if($("#nickname").val().trim() == ""){
                errLog = 1;
                errMsg += "昵称必须填写！\n";
            }
            if($("#birthday").datebox('getValue') == ""){
                errLog = 1;
                errMsg += "出生日期必须填写！\n";
            }
            if($("#email").val().trim() == ""){
                errLog = 1;
                errMsg += "信箱必须填写！\n";
            }
            if($("#pic").val().trim() == ""){
                errLog = 1;
                errMsg += "头像必须选择！\n";
            }
            if(errLog){
                alert(errMsg);
                return;
            }
            $("#uploadPic").ajaxSubmit({
                dataType:"json",
                success:function (d) {
                    if(d.error==1){
                        alert(d.errMsg);
                    }
                    else{
                        postSingup(d.fileName);
                    }
                },
                error:function () {

                }
            })
        }
        function postSingup(fileName){
            $.ajax({
                url:"postSingup.php",
                data:{
                    fileName:fileName,
                    username:$("#username").val().trim(),
                    pw:$("#pw").val().trim(),
                    nickname:$("#nickname").val().trim(),
                    birthday:$("#birthday").datebox('getValue'),
                    email:$("#email").val().trim()
                },
                dataType:"json",
                type:"POST",
                success:function (d) {
                    if(d.error==1){
                        alert(d.errMsg);
                    }
                    else{
                        alert("注册成功");
                        location.reload();
                    }
                },
                error:function (d) {
                    alert("网络出错");
                }
            })
        }
        function getData(startIndex,perPage){
            $.ajax({
                url:"getMainTopic.php",
                data:{startIndex:startIndex,perPage:perPage},
                dataType:"JSON",
                type:"POST",
                success:function (d) {
                    if(d.data == ""){
                        noData = 1;
                        alert("暂无更多数据");
                        return;
                    }
                    startPage += perPage;
                    if(d.error == 1){
                        alert(d.errMsg);
                    }
                    else{
                        //console.log(d.data[0]);
                        $("#total1").text(d.total);
                        var str = "";
                        for(var i in d.data){
                            //console.log(d);
                            //console.log(d.data[i].title);
                            str += "<li style='float:left;width:30px;height: 30px;color:gray;cursor:pointer' class='SETTOP'>";
                            if(d.data[i].istop == 1){
                                if(isAdmin == 1){ //说明是管理员，置顶功能有效
                                    str += "<span class='list' data-last='1' data-id='"+d.data[i].id+"'><img src='img/top.png'></span>";
                                }
                                else{
                                    str += "<span data-last='1' data-id='"+d.data[i].id+"'><img src='img/top.png'></span>";
                                }
                            }
                            else{
                                if(isAdmin == 1){
                                    str += "<span  data-last='0' class='list' data-id='"+d.data[i].id+"'>顶</span>";
                                }
                                else{
                                    str += "<span data-last='0' data-id='"+d.data[i].id+"'>&nbsp;</span>";
                                }
                            }
                            str += "</li>";
                            str += "<li class='li_bot'><ul class='topic2'>";
                            str += "<li style='width:50%' class='topicTitle'>" +
                                "<a href='subtopic.php?id="+d.data[i].id+"'>"+d.data[i].title+"</a></li>";
                            str += "<li style='width:15%'>"+d.data[i].MainNickName+"<br>" +
                                "<span class='posttime'>"+d.data[i].posttime+"</span></li>";
                            str +="";
                            str += "<li style='width:15%'><span class='replyNum'>"+d.data[i].replyNum+
                                "</span><br><span class='posttime'>"+d.data[i].hits+"</span> </li>";
                            str += "<li style='width:15%'>"+d.data[i].SubNickName+"<br><span class='posttime'>"+d.data[i].lastreplyposttime+"</span></li>";
                            str += "</ul><div class=\"clear\"></div></li><div class=\"clear\"></div>";
                        }
                        $("#realContent").append(str);
                        //此时，数据列表已经出现。接下来，需要绑定点击事件
                        settop();
                    }
                },
                error:function () {
                    alert("网络错误");
                }
            })
        }
        function settop(){
            $(".list").unbind('click');//先解绑
            $(".list").click(function () {  //再绑定
                console.log($(this).data());
                var setTop = '';
                var current = $(this);
                if(current.attr('data-last') == 1){ //
                    setTop = 0;
                }
                else{
                    setTop = 1;
                }
                $.ajax({
                    url:"setTop.php",
                    data:{id:current.attr('data-id'),setTop:setTop},
                    type:"POST",
                    dataType:"JSON",
                    success:function (d) {
                        if(d.error == 1){
                            alert(d.errMsg);
                        }
                        else{
                            if(setTop == 0){ //说明点击前它是置顶，现在要将箭头图片换成文字顶
                                current.html("顶");
                                current.attr('data-last',0);
                            }
                            else{ //说明点击前不是置顶，现在要将文字顶换成箭头图片
                                current.html("<img src='img/top.png'>");
                                current.attr('data-last',1);
                            }
                        }
                    },
                    error:function () {
                        alert("操作失败");
                    }
                })
            })
        }
        $(function () {
            //console.log("window height:" + $(window).height());
            //console.log("document height:" + $(document).height());
            $(window).bind("scroll",function () {
                //console.log($(document).scrollTop());
                //console.log("window height:" + $(window).height());
                var s = parseInt($(document).scrollTop()) + parseInt($(window).height());
                //console.log("window + document = " + s);
                //console.log("document height:" + $(document).height());
                if($(document).scrollTop() + $(window).height() >= $(document).height()){
                    //说明到了页面底部了，加载新的数据
                    getMore();
                }
            })
            $("#birthday").datebox({
                required:true,
                panelWidth:200,
                panelHeight:150
            })
            var pic = "<form id='uploadPic' action='picUpload.php' method='post' " +
                "ecntype='mulitpart/form-data'></form>";
            $("#pic").wrap(pic);//在元素外面包裹容器
            $("#slider").responsiveSlides({
                speed:1000
            });
            //检查是否有登录
            //有两种登录方式。
            //第1种是判断session.
            //第2种是判断cookie.
            if(hasLogin){ //说明已经登录了或者是保存了登录
                $.ajax({
                    url:"getInfo.php",
                    data:{username:hasLoggedUserName},
                    type:"post",
                    dataType:"JSON",
                    success:function (d) {
                        if(d.error ==1){
                        }
                        else{
                            isAdmin = d.isAdmin;
                            $("#username1").text(d.username);
                            $("#nickname1").text(d.nickname);
                            $("#singUpDate").text(d.singUpDate);
                            $("#lastTitle").text(d.lastTitle);
                            $("#lastPostTime").text(d.lastPostTime);
                            $("#total").text(d.total);
                            $("#lastReplyTime").text(d.lastReplyTime);
                            $(".tx img").attr('src','tx/' + d.pic);
                            $(".info").show();
                            showLogout();
                            //读取首页数据
                            getData(startPage, perPage);
                        }
                    },
                    error:function (d) {
                        getData(startPage, perPage);
                        alert("网络超时");
                    }
                })
            }
            else{
                getData(startPage, perPage);
            }
            $("#login").click(function () {
                easyDialog.open({
                    container:'loginFrom',
                    fixed:false
                })
            })
            $("#showRegister").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container:'registerForm',
                    fixed: false
                })
            })
            $("#showLogin").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container:'loginFrom',
                    fixed: false
                })
            })
        })
        function login() {
            if($("#username2").val() == "" || $("#pw2").val() == ""){
                alert("用户名和密码必须填写！");
                return;
            }
            rememberMe = $("#rememberMe").prop("checked") ? "1":"0";
            $.ajax({
                url:"postLogin.php",
                data:{
                    username:$("#username2").val(),
                    pw:$("#pw2").val(),
                    rememberMe:rememberMe},
                type:"post",
                dataType:"JSON",
                success:function (d) {
                    if(d.error ==1){
                        alert(d.errMsg);
                    }
                    else{
                        $("#username1").text(d.username);
                        $("#nickname1").text(d.nickname);
                        $("#singUpDate").text(d.singUpDate);
                        $("#lastTitle").text(d.lastTitle);
                        $("#lastPostTime").text(d.lastPostTime);
                        $("#total").text(d.total);
                        $("#lastReplyTime").text(d.lastReplyTime);
                        $("#username2").val('');
                        $("#pw2").val('');
                        $(".tx img").attr('src','tx/' + d.pic);
                        $(".info").show();
                        easyDialog.close();
                        showLogout();
                    }
                },
                error:function (d) {
                    alert("网络超时");
                }
            })
        }
    </script>
</body>
</html>